package dingding.使用线程池;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Demo1 {

    private final static int AVALABLE_PROCESSORS = Runtime.getRuntime().availableProcessors();

    private final static ThreadPoolExecutor pool = new ThreadPoolExecutor(AVALABLE_PROCESSORS,
            AVALABLE_PROCESSORS * 2, 1, TimeUnit.MINUTES,
            new LinkedBlockingDeque<>(5),
            new ThreadPoolExecutor.CallerRunsPolicy());

    public static void main(String[] args) throws InterruptedException {
        Long start = System.currentTimeMillis();
        pool.execute(() -> {
            doSomethingA();
        });
        doSomethingB();

        Long end = System.currentTimeMillis();
        System.out.println(end - start);
        Thread.currentThread().join();
    }


    public static void doSomethingA() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("-----doSomethingA");
    }

    public static void doSomethingB() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("-----doSomethingB");
    }
}
